package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author gatsby
 * @since 2022/10/25 22:14
 */
@RestController
public class OrderController {
    @Autowired
    OrderCoreService orderCoreService;

    @PostMapping("/shopping/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request,HttpServletRequest servletRequest){
        //获取请求头
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        //获取用户id
        Long userId =Long.parseLong(object.get("uid").toString());
        request.setUserId(userId);
        String orderId = orderCoreService.createOrder(request);
        ResponseData<String> responseData = new ResponseData<>();
        responseData.setCode(200);
        responseData.setMessage("success");
        responseData.setResult(orderId);
        responseData.setSuccess(true);


        return responseData;



    }

    @GetMapping("/shopping/order")
    public ResponseData queryAllOrder(OrderListRequest request,HttpServletRequest servletRequest){
        OrderListRequest orderListRequest = new OrderListRequest();
        //获取用户id
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long userId=Long.parseLong(object.get("uid").toString());
        orderListRequest.setUserId(userId);
        orderListRequest.setPage(request.getPage());
        orderListRequest.setSize(request.getSize());
        OrderListResponse orderListResponse=orderCoreService.queryAllOrder(orderListRequest);
        ResponseData<OrderListResponse> responseData = new ResponseData<>();
        responseData.setSuccess(true);
        responseData.setResult(orderListResponse);
        responseData.setMessage("success");
        responseData.setCode(200);
        return responseData;

    }

    @GetMapping("/shopping/order/{id}")
    public ResponseData queryOrderDetail( @PathVariable("id") String id ){
        OrderDetailResponse orderDetailResponse=orderCoreService.queryOrderDetail(id);
        ResponseData<OrderDetailResponse> responseData = new ResponseData<>();
        responseData.setCode(200);
        responseData.setResult(orderDetailResponse);
        responseData.setSuccess(true);
        return responseData;
    }

    @PostMapping("/shopping/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map){
        String orderId = (String) map.get("orderId");
        orderCoreService.cancelOrder(orderId);
        return new ResponseUtil().setData(null);
    }

    @DeleteMapping("/shopping/order/{id}")
    public ResponseData removeOrder(@PathVariable("id") String id){
        orderCoreService.deleteOrder(id);
        return new ResponseUtil().setData(null);
    }
}
